package com.ay.redisqps.service;

import com.ay.redisqps.dao.TicketRepository;
import com.ay.redisqps.pojo.Ticket;
import com.ay.redisqps.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/* *
 * Created by Ay on 2018/11/13
 */
@Service
public class TicketService {

    @Autowired
    TicketRepository ticketRepository;


    @Autowired
    RedisUtil redis;
    public Integer findTicketByName(String name){
        //1.先从缓存获取
        String value = redis.get(name);
        if(value != null){
            System.out.println(Thread.currentThread().getId()+"从缓存获取："+value);
            return Integer.valueOf(value);
        }
        //2.查询数据库
        Ticket ticket = ticketRepository.findByName(name);
        System.out.println(Thread.currentThread().getId()+"从数据库获取："+ticket.getTickets());
        //3.放入缓存
        redis.set(name,ticket.getTickets(),120);
        return 0;
    }
}
